package com.youdianstar.app.ui.pay

import androidx.databinding.ObservableField
import androidx.lifecycle.viewModelScope
import androidx.paging.PagingData
import androidx.paging.cachedIn
import com.youdianstar.app.common.base.BaseViewModel
import com.youdianstar.app.entity.WalletInfo
import com.youdianstar.app.common.event.NetEventLiveData
import com.youdianstar.app.common.event.NetStateLiveData
import com.youdianstar.app.common.util.LogUtil
import com.youdianstar.app.entity.CardDefaultReq
import com.youdianstar.app.entity.PaymentRecord
import com.youdianstar.app.entity.ReceiptReq
import com.youdianstar.app.entity.CardUnbindRes
import com.youdianstar.app.support.ApiRepository

import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.launch


/**
 * File: WalletViewModel
 * Author: 82149 Create: 2021/11/24 11:28
 * Changes (from 2021/11/24)
 * --------------------------------------------------
 * description:
 * ---------------------------------------------------
 *
 */
class WalletViewModel(private val repo: ApiRepository) : BaseViewModel() {

    val walletLiveData = NetStateLiveData<WalletInfo>()
    val walletDataFiled = ObservableField<WalletInfo>()

    fun getWallet(isShow: Boolean) {
        if (isShow)
            showLoading()
        viewModelScope.launch(Dispatchers.IO) {
            repo.getWallet(walletLiveData)
        }
    }

    val listEventLiveData = NetEventLiveData()
    /**
     * 交易流水列表
     */
    fun getRecordList(): Flow<PagingData<PaymentRecord>>? {
        return try {
            repo.getBillRecords(listEventLiveData).cachedIn(viewModelScope)
        } catch (e :Exception){
            e.printStackTrace()
            listEventLiveData.postValue(NetEventLiveData.Result.NET_ERROR)
            null
        }
    }

    val detailResultLiveData = NetStateLiveData<PaymentRecord>()

    /**
     * 支付-流水详情
     *
     * @param transNo 流水号
     */
    fun getBillRecordDetail(transNo:String){
        showLoading()
        viewModelScope.launch (Dispatchers.IO){
            repo.getBillDetail(transNo ,detailResultLiveData)
        }
    }


    val receiptResultLiveData = NetStateLiveData<Any>()
    /**
     * 发送发票
     * @param billID 流水id
     */
    fun sendPaymentReceipt(billID:Int, orderNo:String, email : String){
        showLoading()
        viewModelScope.launch (Dispatchers.IO){
            repo.sendReceipts(ReceiptReq(id = billID,orderNo,email),receiptResultLiveData)
        }
    }

    val refundLiveData = NetStateLiveData<CardUnbindRes>()
    /**
     * 退款 提现
     */
    fun requestRefund() {
        showLoading()
        viewModelScope.launch(Dispatchers.IO) {
            repo.requestRefund(refundLiveData)
        }
    }

    val unBindLiveData = NetStateLiveData<CardUnbindRes>()

    /**
     * 解绑
     */
    fun unBindCard(id: Int) {
        LogUtil.i("-----unbind : " + id)
        viewModelScope.launch(Dispatchers.IO) {
            repo.unBindCard(id, unBindLiveData)
        }
    }

    val defaultLiveData = NetStateLiveData<Any>()

    /**
     * 默认卡设置
     */
    fun setDefaultCard(id: Int, isDefault: Boolean) {
        LogUtil.i("-----setDefaultCard : " + id + "; isDefault = " + isDefault)
        viewModelScope.launch(Dispatchers.IO) {
            repo.setDefaultCard(CardDefaultReq(id, if (isDefault) 1 else 2), defaultLiveData)
        }
    }
}